package com.coocaa.app.lib.cleaner.scanner;

import android.content.Context;

import com.coocaa.app.lib.cleaner.mgr.DataHolder;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lu on 17-4-12.
 */

public class ScanRunnerTest {

    @Before
    public void setUp() throws Exception {

    }

    @Test
    public void onScanStartEndCallback() throws Exception {
        ScanRunner runner = ScanRunnerFactory.factory.create();
        ScanRunner.ScanRunnerListener listener = Mockito.mock(ScanRunner.ScanRunnerListener.class);
        List<AbstractScanner> runs = new ArrayList<AbstractScanner>();

        class A implements AbstractScanner {
            @Override
            public void setDataHolder(Context context, DataHolder holder) {

            }

            @Override
            public void stop() {

            }

            @Override
            public void run() {
                try {
                    Thread.sleep((long) (Math.random() * 5000) + 500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        for (int i = 0; i < 5; i++) {
            runs.add(new A());
        }

        Assert.assertTrue(runner.start(listener, runs));
        Assert.assertFalse(runner.start(listener, runs));

        Thread.sleep(10000);

        Mockito.verify(listener, Mockito.times(1)).onScanStart();
        for (AbstractScanner r : runs)
            Mockito.verify(listener, Mockito.times(1)).onScanEnd(r);
        Mockito.verify(listener, Mockito.times(1)).onScanAllEnd();
    }
}
